import Cookie from 'js-cookie'
import { apigetUserInfo } from '../api'
import Store from '../store'

export const getCookie = (CookieName: string = 'token') => {
    return Cookie.get(CookieName)
}

//  获取用户信息
export const getUserInfo = () => {
    if(getCookie()) {
        const { dispatch } = Store;

        apigetUserInfo().then(res => {
            dispatch({type: 'CHANGE_USER_INFO', data: res.data})
        }).catch(() => {
            // 删除本地token 
            Cookie.remove('token')

            // 跳转到登录页面
            window.location.href = '/login'
        })
    }
}